package com.winit.openapi.wh.outbound.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.common.query.Page;
import com.winit.common.query.Searchable;
import com.winit.common.query.Sort;
import com.winit.common.query.Sort.Direction;
import com.winit.erp.spi.exwarehouse.ExWarehouseService;
import com.winit.erp.spi.exwarehouse.command.QueryExWarehouseCommand;
import com.winit.erp.spi.exwarehouse.vo.ExWarehouseProduct;
import com.winit.oms.spi.outbound.OutboundOrderMerchandiseService;
import com.winit.oms.spi.outbound.OutboundOrderService;
import com.winit.oms.spi.outbound.command.order.DeleteOrderMerchandiseCommand;
import com.winit.oms.spi.outbound.command.order.QueryOrderMerchandiseByOrderNoCommand;
import com.winit.oms.spi.outbound.command.order.UpdateOrderMerchandiseCommand;
import com.winit.oms.spi.outbound.vo.OutboundOrderVo;
import com.winit.oms.spi.outbound.vo.order.OutboundOrderMerchandiseVo;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.ValidateUtil;
import com.winit.openapi.wh.outbound.builder.OutboundOrderMerchandiseBuilder;
import com.winit.openapi.wh.outbound.validator.OutboundOrderMerchandiseValidator;

@Controller
@RequestMapping(value = "/wh/outbound")
public class OutboundOrderMerchandiseController extends BaseController {

    @Resource
    private OutboundOrderMerchandiseService outboundOrderMerchandiseService;
    @Resource
    private ExWarehouseService              exWarehouseService;
    @Resource
    private OutboundOrderService            outboundOrderService;
    
    
    @RequestMapping(value = "/deleteMerchandise")
    public String deleteMerchandise(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        DeleteOrderMerchandiseCommand command = new DeleteOrderMerchandiseCommand();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            ValidateUtil.isNotNull(json, "merchandiseIdList");
            JSONArray array = json.getJSONArray("merchandiseIdList");
            if (array == null || array.size() == 0) {
                throw new ApiException(ErrorCode.FIELD_NOT_NULL, "merchandiseIdList");
            }
            List<Long> merchandiseIdList = new ArrayList<Long>();
            for (Object item : array) {
                JSONObject jsonobj = (JSONObject) item;
                ValidateUtil.isNotNull(jsonobj, "merchandiseId");
                ValidateUtil.validMaxLength(jsonobj, "merchandiseId", 64, true);
                merchandiseIdList.add(jsonobj.getLong("merchandiseId"));
            }
            command.setMerchandiseIdList(merchandiseIdList);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        outboundOrderMerchandiseService.deleteMerchandise(command);
        responseMsg.setMsg("SUCCESS");
        return SUCCESS;
    }

    @RequestMapping(value = "/modifyMerchandiseByEbay")
    public String modifyMerchandiseByEbay(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        UpdateOrderMerchandiseCommand command = new UpdateOrderMerchandiseCommand();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            OutboundOrderMerchandiseValidator.validUpdateMerchandise(json);
            OutboundOrderVo orderVo = new OutboundOrderVo();
            if (json.containsKey("orderNo")) {
                orderVo.setOrderNo(json.getString("orderNo"));
            }
            if (json.containsKey("isAcrossWarehouse")) {
                orderVo.setIsAcrossWarehouse(json.getString("isAcrossWarehouse"));
            }
            orderVo.setMerchandiseInfoList(OutboundOrderMerchandiseBuilder.buildListMerchandise(json));
            command.setOrderVo(orderVo);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        outboundOrderService.batchUpdateMerchandise(command);
        responseMsg.setMsg("SUCCESS");
        return SUCCESS;
    }

    @RequestMapping(value = "/getMerchandsieList")
    public String getMerchandsieList(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        QueryOrderMerchandiseByOrderNoCommand command = new QueryOrderMerchandiseByOrderNoCommand();
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            ValidateUtil.validNotNull(json, "orderNo");
            ValidateUtil.validMaxLength(json, "orderNo", 64);
            command.setOrderNo(json.getString("orderNo"));
            Searchable searchable = buildSearchablePageParams(json);
            searchable.addSort(new Sort(Direction.DESC, "CREATED"));
            command.setSearchable(searchable);
        } else if (obj == null || StringUtils.isBlank(obj.toString())) {
            Searchable searchable = buildSearchablePageParams(null);
            command.setSearchable(searchable);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        Page<OutboundOrderMerchandiseVo> pagelist = outboundOrderMerchandiseService.getRmaMerchandiseByOrder(command);
        if (pagelist != null && pagelist.getContent() != null && pagelist.getContent().size() > 0) {
            List<Map<String, Object>> datalist = OutboundOrderMerchandiseBuilder.buildMerchandiseList(pagelist.getContent());
            Map<String, Object> pageMap = getPageParams(pagelist);
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("merchandiseInfoList", datalist);
            data.put("pageParams", pageMap.get("pageParams"));
            responseMsg.setData(data);
        }
        return SUCCESS;
    }

    @RequestMapping(value = "/queryMerchandiseList")
    public String queryMerchandiseList(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        QueryOrderMerchandiseByOrderNoCommand command = new QueryOrderMerchandiseByOrderNoCommand();
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            ValidateUtil.validNotNull(json, "orderNo");
            ValidateUtil.validMaxLength(json, "orderNo", 64);
            command.setOrderNo(json.getString("orderNo"));
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        List<OutboundOrderMerchandiseVo> merlist = outboundOrderMerchandiseService.getMerchandiseList(command);
        if (merlist != null && merlist.size() > 0) {
            List<Map<String, Object>> datalist = OutboundOrderMerchandiseBuilder.buildMerchandiseList(merlist);
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("merchandiseInfoList", datalist);
            responseMsg.setData(data);
        }
        return SUCCESS;
    }
    
    @RequestMapping(value = "/getMerchandiseAndItemList")
    public String getMerchandiseAndItemList(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        QueryOrderMerchandiseByOrderNoCommand command = new QueryOrderMerchandiseByOrderNoCommand();
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            ValidateUtil.validNotNull(json, "outboundOrderNo");
            ValidateUtil.validMaxLength(json, "outboundOrderNo", 64);
            command.setOrderNo(json.getString("outboundOrderNo"));
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        String orderNo = command.getOrderNo();
        if(orderNo.contains("WO")){
        	List<OutboundOrderMerchandiseVo> merlist = outboundOrderMerchandiseService.getMerchandiseAndItemByOrder(command);
            if (merlist != null && merlist.size() > 0) {
                List<Map<String, Object>> datalist = OutboundOrderMerchandiseBuilder.buildMerchandiseAndItemList(merlist);
                Map<String, Object> data = new HashMap<String, Object>();
                data.put("merchandiseInfoList", datalist);
                responseMsg.setData(data);
            }
        }else{
        	QueryExWarehouseCommand ecommand = new QueryExWarehouseCommand();
        	ecommand.setCode(orderNo);
        	List<ExWarehouseProduct> exProductList = exWarehouseService.queryExwarehosueProdcutDetailByOrderNo(ecommand);
        	List<Map<String, Object>> datalist = OutboundOrderMerchandiseBuilder.buildExMerchandiseAndItemList(exProductList);
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("merchandiseInfoList", datalist);
            responseMsg.setData(data);
        }
        return SUCCESS;
    }
}
